package com.seleniti.enpareja.android.ui.view;

import org.ioc.commons.impl.android.common.util.ListViewPagedDataProviderAdapter;
import org.ioc.commons.impl.android.ui.HasEnabledWrapper;
import org.ioc.commons.impl.android.ui.HasVisibilityWrapper;
import org.ioc.commons.model.dataprovider.PagedDataProvider;
import org.ioc.commons.ui.HasVisibility;
import org.ioc.commons.ui.features.Info;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.seleniti.enpareja.android.R;
import com.seleniti.enpareja.android.ui.widget.InfoInlineWidget;
import com.seleniti.enpareja.controller.model.integration.database.domain.CoupleRequest;
import com.seleniti.enpareja.controller.ui.view.GetCoupleView;
import com.seleniti.enpareja.controller.ui.view.GetCoupleView.Action;
import com.seleniti.enpareja.controller.ui.view.GetCoupleView.Event;
import com.seleniti.enpareja.controller.ui.view.GetCoupleView.Operation;
import com.seleniti.enpareja.controller.ui.view.GetCoupleView.Presenter;
import com.seleniti.enpareja.controller.ui.view.GetCoupleViewPresenter;

public class GetCoupleViewWidget extends EnParejaBaseView<Event, Operation, Action, Presenter> implements GetCoupleView {

	private ListView lstCoupleRequests;
	private InfoInlineWidget coupleGotInfo;
	private boolean loading;
	private ListViewPagedDataProviderAdapter<CoupleRequest, Operation> pagedDataProvider;
	private int selected = -1;
	private Button btnGetCoupleNow;
	private Button btnReject;
	private Button btnDismissRequest;
	private Button btnRequestAgain;
	private Button btnDismissRequest2;
	private LinearLayout pendingActions;
	private LinearLayout stillPendingActions;
	private LinearLayout rejectedActions;

	public GetCoupleViewWidget() {
		super(true);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setRetainInstance(true);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		this.presenter = new GetCoupleViewPresenter(this, eventbus, operationManager, actionController);		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.get_couple_view, container, false);

		this.lstCoupleRequests = (ListView) view.findViewById(R.id.lstCoupleRequests);
		this.coupleGotInfo = (InfoInlineWidget) view.findViewById(R.id.coupleGotInfo);
		this.btnGetCoupleNow = (Button) view.findViewById(R.id.btnGetCoupleNow);
		this.btnReject = (Button) view.findViewById(R.id.btnReject);
		this.btnDismissRequest = (Button) view.findViewById(R.id.btnDismissRequest);
		this.btnRequestAgain = (Button) view.findViewById(R.id.btnRequestAgain);
		this.btnDismissRequest2 = (Button) view.findViewById(R.id.btnDismissRequest2);
		this.pendingActions = (LinearLayout) view.findViewById(R.id.pendingActions);
		this.stillPendingActions = (LinearLayout) view.findViewById(R.id.stillPendingActions);
		this.rejectedActions = (LinearLayout) view.findViewById(R.id.rejectedActions);

		TextView noResults = (TextView) view.findViewById(R.id.noResults);

		this.lstCoupleRequests.setTextFilterEnabled(true);
		this.lstCoupleRequests.setEmptyView(noResults);
		this.lstCoupleRequests.setSelector(R.drawable.list_selector);
		this.lstCoupleRequests.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				boolean notify = GetCoupleViewWidget.this.selected != position;
				GetCoupleViewWidget.this.selected = position;

				if (notify && pagedDataProvider != null) {
					pagedDataProvider.notifyDataSetChanged();
					presenter.onSelectedCoupleRequestChanged();
				}
			}
		});

		OnClickListener dismiss = new OnClickListener() {

			@Override
			public void onClick(View v) {
				presenter.onDismissSelectedRequest();
			}
		};
		this.btnDismissRequest.setOnClickListener(dismiss);
		this.btnDismissRequest2.setOnClickListener(dismiss);

		this.btnReject.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				presenter.onRejectSelectedRequest();
			}
		});
		this.btnGetCoupleNow.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				presenter.onGetCoupleNow();
			}
		});

		return view;
	}

	private static class ItemViewHolder {

		protected TextView from;
		protected TextView to;
		protected TextView status;

	}

	@Override
	public void setPagedDataProvider(PagedDataProvider<CoupleRequest> dataProvider) {
		int resource = 0;
		int textResource = 0;
		this.pagedDataProvider = new ListViewPagedDataProviderAdapter<CoupleRequest, Operation>(this.lstCoupleRequests,
				dataProvider, resource, textResource, Operation.GettingCoupleRequests, operationManager, this) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				ItemViewHolder ivh = null;
				if (convertView == null) {

					Bundle savedInstanceState = new Bundle();
					LayoutInflater inflater = getLayoutInflater(savedInstanceState);
					convertView = inflater.inflate(R.layout.couple_request_layout, parent, false);

					ivh = new ItemViewHolder();

					ivh.from = (TextView) convertView.findViewById(R.id.from);
					ivh.to = (TextView) convertView.findViewById(R.id.to);
					ivh.status = (TextView) convertView.findViewById(R.id.status);

					convertView.setTag(ivh);
				} else {
					ivh = (ItemViewHolder) convertView.getTag();
				}

				CoupleRequest object = this.getItem(position);
				String from = object != null && object.getFrom() != null && object.getFrom().getUserEmail() != null
						&& !object.getFrom().getUserEmail().isEmpty() ? object.getFrom().getUserEmail()
						: "-- Sin email --";
				String to = object != null && object.getTo() != null && object.getTo().getUserEmail() != null
						&& !object.getTo().getUserEmail().isEmpty() ? object.getTo().getUserEmail() : "-- Sin email --";
				String status = object != null && object.getStatus() != null ? object.getStatus().getLabel()
						: "-- No se sabe --";
				ivh.from.setText(from);
				ivh.to.setText(to);
				ivh.status.setText(status);

				if (selected != -1 && selected == position) {
					convertView.setSelected(true);
					convertView.setBackgroundColor(Color.parseColor("#628CD5"));
				} else {
					convertView.setSelected(false);
					convertView.setBackgroundColor(Color.WHITE);
				}

				return convertView;
			}
		};
	}

	@Override
	public PagedDataProvider<CoupleRequest> getPagedDataProvider() {
		return this.pagedDataProvider;
	}

	@Override
	public void setLoading(boolean loading) {
		this.loading = loading;
		HasEnabledWrapper.setEnabled(!loading, btnGetCoupleNow, btnReject, btnDismissRequest, btnDismissRequest2,
				btnRequestAgain);
	}

	@Override
	public boolean isLoading() {
		return this.loading;
	}

	@Override
	public void startSearch() {
		pagedDataProvider.reload();
	}

	@Override
	public CoupleRequest getSelectedCoupleRequest() {
		CoupleRequest selectedItem = selected < 0 ? null : (CoupleRequest) this.lstCoupleRequests
				.getItemAtPosition(selected);

		return selectedItem;
	}

	@Override
	public Info coupleGotInfo() {
		return this.coupleGotInfo;
	}

	@Override
	public void clearSelection() {
		this.lstCoupleRequests.clearChoices();

	}

	@Override
	public HasVisibility pendingActions() {
		return HasVisibilityWrapper.from(pendingActions, this);
	}

	@Override
	public HasVisibility stillPendingActions() {
		return HasVisibilityWrapper.from(stillPendingActions, this);
	}

	@Override
	public HasVisibility rejectedActions() {
		return HasVisibilityWrapper.from(rejectedActions, this);
	}

}
